package cn.iocoder.yudao.hires.controller.admin;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.hires.entity.DictCityDO;
import cn.iocoder.yudao.hires.service.DictCityService;
import cn.iocoder.yudao.hires.vo.DictCityPageReqVO;
import cn.iocoder.yudao.hires.vo.DictCityRespVO;
import cn.iocoder.yudao.hires.vo.DictCitySaveReqVO;
import cn.iocoder.yudao.hires.vo.DictCityTreeNodeVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 行政区划")
@RestController
@RequestMapping("/resume/dict-city")
@Validated
public class DictCityController {

    @Resource
    private DictCityService dictCityService;

    @Resource
    private RedisTemplate redisTemplate;

    @PostMapping("/create")
    @Operation(summary = "创建行政区划")
    @TenantIgnore
    @PermitAll
    public CommonResult<String> createDictCity(@Valid @RequestBody DictCitySaveReqVO createReqVO) {
        return success(dictCityService.createDictCity(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新行政区划")
    @TenantIgnore
    @PermitAll
    public CommonResult<Boolean> updateDictCity(@Valid @RequestBody DictCitySaveReqVO updateReqVO) {
        dictCityService.updateDictCity(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除行政区划")
    @TenantIgnore
    @PermitAll
    public CommonResult<Boolean> deleteDictCity(@RequestParam("id") String id) {
        dictCityService.deleteDictCity(id);
        return success(true);
    }

    @TenantIgnore
    @PermitAll
    @GetMapping("/get")
    @Operation(summary = "获得行政区划")
    public CommonResult<DictCityRespVO> getDictCity(@RequestParam("id") String id) {
        DictCityDO dictCity = dictCityService.getDictCity(id);
        return success(BeanUtils.toBean(dictCity, DictCityRespVO.class));
    }

    @TenantIgnore
    @PermitAll
    @GetMapping("/page")
    @Operation(summary = "获得行政区划分页")
    public CommonResult<PageResult<DictCityRespVO>> getDictCityPage(@Valid DictCityPageReqVO pageReqVO) {
        PageResult<DictCityDO> pageResult = dictCityService.getDictCityPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DictCityRespVO.class));
    }

    @TenantIgnore
    @PermitAll
    @GetMapping("/export-excel")
    @Operation(summary = "导出行政区划 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDictCityExcel(@Valid DictCityPageReqVO pageReqVO, HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DictCityDO> list = dictCityService.getDictCityPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "行政区划.xls", "数据", DictCityRespVO.class, BeanUtils.toBean(list, DictCityRespVO.class));
    }

    @TenantIgnore
    @PermitAll
    @GetMapping("/tree")
    @Operation(summary = "行政区域树形结构")
    public CommonResult<List<DictCityTreeNodeVO>> getCategoryTree() {
        // 1. 尝试从Redis获取缓存
        String cacheKey = "system:dict:category:tree";
        List<DictCityTreeNodeVO> cachedTree = (List<DictCityTreeNodeVO>) redisTemplate.opsForValue().get(cacheKey);
        if (cachedTree != null) {
            return success(cachedTree);
        }
        List<DictCityDO> categories = dictCityService.getList();
        List<DictCityTreeNodeVO> tree = dictCityService.buildCategoryTree(categories);
        redisTemplate.opsForValue().set("system:dict:category:tree", tree, 12, TimeUnit.HOURS);
        return success(tree);
    }
}